import { FairyGUI, System } from "csharp";

/**
 * XML转换为JSON
 */
export default class XML2JSON {

    private static _arrays: Array<any> = null;

    public static parse(node: FairyGUI.Utils.XML): Object {

        console.log( "[parse]----------->node.name:" +node.name + " node.elements.Count: "+node.elements.Count +" node.attributes.Count: " + node.attributes.Count );

        let obj: any = {};
        let numOfChilds: number = node.elements.Count;// node.elements.Count;
        let key: any;
        for (let i: number = 0; i < numOfChilds; i++) {
            let childNode: FairyGUI.Utils.XML = node.elements.get_Item(i);// node.children()[i];
            let childNodeName: string = childNode.name;
            let value: any;
            if (childNode.elements.Count == 1 && childNode.elements.get_Item(0).name == null) {
                if (childNode.attributes.Count > 0) {
                    value = {// childNode.children()[0].toString()
                        _content: childNode.elements.get_Item(0).ToString()
                    };
                    // let  numOfAttributes: number = childNode.attributes.Count;
                    // for (let  j: number = 0; j < numOfAttributes; j++) {
                    //     // let item = childNode.attributes.get_Item(j)
                    //     value[childNode.attributes.get_Item(j+"") ] = childNode.attributes.get_Item(j+"");
                    // }
                } else {
                    value = childNode.elements.get_Item(0).ToString();// .children()[0].toString();
                }
            } else {
                value = this.parse(childNode);
            }
            if (obj[childNodeName]) {
                if (this.getTypeof(obj[childNodeName]) == "array") {
                    obj[childNodeName].push(value);
                } else {
                    obj[childNodeName] = [obj[childNodeName], value];
                }
            } else if (this.isArray(childNodeName)) {
                obj[childNodeName] = [value];
            } else {
                obj[childNodeName] = value;
            }
        }
        let numOfAttributes = node.attributes.Count;
        // for (i = 0; i < numOfAttributes; i++) {
        //     // obj[node.attributes()[i].name().toString()] = node.attributes()[i];
        // }
        // console.log( "[obj]----------->node.name:" +node.name + " numOfAttributes: " + numOfAttributes +"----------");
        // Object.entries(node.attributes).forEach(
        //     ([key, value]) =>{
        //         console.log("[xxxxx]---------->" + key, value);
        //     }
        // );    

        let keyStr:any;
        let keys:any = Object.entries(node.attributes);
        console.log("[yyyyy]---------->keys: " + keys);
        // for( let i=0;i<keys.Length;i++  ) { //这里如何遍历????
        for( key in keys ){
            // keyStr = keys.get_Item(i);
            let val = node.attributes.ContainsKey(keyStr) ? node.attributes.get_Item(keyStr) : null;
            obj[key] = val;
            console.log( "[obj]222----------->key: " + keyStr + " value: " + val );
        }
        if (numOfChilds == 0) {
            if (numOfAttributes == 0) {
                obj = "";
            } else {
                obj._content = "";
            }
        }
        return obj;
    }

    public static get arrays(): Array<any> {
        if (!this._arrays) {
            this._arrays = [];
        }
        return this._arrays;
    }

    public static set arrays(a: Array<any>) {
        this._arrays = a;
    }

    private static isArray(nodeName: string): boolean {
        let numOfArrays: number = this._arrays ? this._arrays.length : 0;
        for (let i: number = 0; i < numOfArrays; i++) {
            if (nodeName == this._arrays[i]) {
                return true;
            }
        }
        return false;
    }

    private static getTypeof(o: any): string {
        if (typeof (o) == "object") {
            if (o.length == null) {
                return "object";
            } else if (typeof (o.length) == "number") {
                return "array";
            } else {
                return "object";
            }
        } else {
            return typeof (o);
        }
    }
}
